<?php 
class BuildPrint extends Module
{
	static $bcatid = 0,$bcat = array(),$cats = array(), $items = array(), $price = 0;
	
	function BuildPrint($row)
	{
		Module::Module($row);
		
		##################################################################################################
		$build_catids = @CGlobal::$configs['BuildCatIDs']['conf_val'];
		
		if($build_catids != '')
		{
			$arr 			= explode(',',$build_catids);
			$build_catids 	= '';
			
			if($arr)
			{
				foreach ($arr as $catid)
				{
					if(isset(CGlobal::$allCategories[$catid]) && CGlobal::$allCategories[$catid]['status'] != 'HIDE')
					{
						$build_catids	.=	($build_catids!='' ? ',' : '')	.	$catid;
						
						BuildPrint::$cats[$catid] = array(
														'id'			=>	$catid,
														'name'			=>	CGlobal::$allCategories[$catid]['name'],
														'img' 			=> AZLib::getImageThumb("category/$catid.gif",40,40,0,0),
														'item_id' 		=> 0,
														'item_name' 	=> '',
														'item_url' 		=> '',
														'item_price' 	=> '',
														'item_warranty' => '',
														'list_brief' 	=> '',
														);
					}
				}
			}
		}
		
		self::$bcatid = (int)Url::get('bcatid');
		
		if(self::$bcatid)
		{
			if(isset(BuildPrint::$cats[self::$bcatid]))
			{
				self::$bcat = BuildPrint::$cats[self::$bcatid];
			}
			else//KO tồn tại dmục này
			{
				Url::redirect_current(array('mode'));
			}
		}
		else 
		{
			self::$bcat = reset(BuildPrint::$cats);
			
			if(self::$bcat)
			{
				self::$bcatid = (int)self::$bcat['id'];
			}
		}
		##################################################################################################
		
		$item_ids	= '';
		
		$build_ids 	= isset($_SESSION['build_ids']) ? $_SESSION['build_ids'] : '';
		
		$build_price 			= 0;
		
		if($build_ids != '')
		{
			$items 			= array();
			
			$re = DB::query("SELECT * FROM item WHERE id IN(".$build_ids.") AND status=1 AND price>0 AND quantity>0 ORDER BY find_in_set(category_id,'".$build_catids."')");
		
			if($re)
			{
				while($item = mysql_fetch_assoc($re))
				{
					if(isset(BuildPrint::$cats[$item['category_id']]) && BuildPrint::$cats[$item['category_id']]['item_id'] == 0)
					{
						$item['price'] = AZLib::convertCurrency($item['price'], $item['currency_id']);
						
						BuildPrint::$price += $item['price'];
						$item_ids	.=	($item_ids!='' ? ',' : '')	.	$item['id'];
						
						if(isset(CGlobal::$allCategories[$item['category_id']])){
							$item_url = WEB_DIR.CGlobal::$allCategories[$item['category_id']]['nice_name']."/p{$item['id']}/".AZLib::safe_title($item['name']).".html";
						}
						else{	
							$item_url = WEB_DIR."p{$item['id']}/".AZLib::safe_title($item['name']).".html";
						}
						
						if($item['list_brief'] == '')
						{
							$item['list_brief'] = $item['brief'];
						}
						
						$item['list_brief'] 	= preg_replace("/\\[([\s]*[0-9]{1,2}[\s]*)\]/eis", " ",$item['list_brief']);
						$item['list_brief']  	= str_replace(array("'","\""), array(""," "),$item['list_brief']);				
						$item['list_brief'] 	= AZLib::word_limit($item['list_brief'],50,'');
						
						BuildPrint::$items[$item['id']]	= $item;
							
						BuildPrint::$cats[$item['category_id']]['item_id']			= 	$item['id'];									
						BuildPrint::$cats[$item['category_id']]['item_name']		= 	$item['name'];									
						BuildPrint::$cats[$item['category_id']]['item_url']			= 	$item_url;									
						BuildPrint::$cats[$item['category_id']]['item_price']		= 	number_format($item['price'],0,',','.');									
						BuildPrint::$cats[$item['category_id']]['item_warranty']	= 	$item['warranty'];								
						BuildPrint::$cats[$item['category_id']]['list_brief']		= 	$item['list_brief'];								
					}
				}
			}
		}
		
		$_SESSION['build_ids'] = "$item_ids";
		
		require_once 'forms/BuildPrint.php';			
		$this->add_form(new BuildPrintForm());
	}
}
?>